package com.sx.micowell.framework.model

import android.annotation.SuppressLint
import androidx.lifecycle.MutableLiveData
import com.example.mvvm.base.vm.BaseViewModel
import com.miloyu.modelnet.network.ApiResponse
import com.miloyu.modelnet.network.HttpClient
import com.miloyu.modelnet.network.service.IApiService
import com.miloyu.modelnet.network.tools.RxUtils
import com.miloyu.modelnet.response.AppBanner
import com.miloyu.modelnet.response.CourseTypeListBean
import com.miloyu.modelnet.response.getUrl
import com.sx.micowell.framework.binding.impl.IBindingAction
import io.reactivex.Observable
import com.sx.micowell.framework.binding.BindingCommand as BindingCommand1


/**
 * @Author: YY
 * @Time: 2021/9/14
 * @Describe TODO
 */
class CourseTrainEndModel: BaseViewModel() {

    //获取训练结束背景
    var imageUrl = MutableLiveData<String>()

    // 完成
    val completeOnClickCommand = BindingCommand1<Any>(IBindingAction {
        context.finish()
    })

    @SuppressLint("CheckResult")
    fun reqData(type: String, id: Int, trainTime: Int, calorie:String) {
        addSubscribe(
            Observable.merge(
                HttpClient.instance.apiService.reportCourseTrain(type, id, trainTime, calorie),
                HttpClient.instance.apiService.getSportEndVideo()
                )
                .doOnSubscribe { ui.showDialog() }
                .compose(RxUtils.compose())
                .subscribeWith(object : ApiResponse<Any>() {
                    override fun success(data: Any?, msg: String?) {
                        when(data) {
                            is AppBanner -> {
                                if (data.info != null) {
                                    imageUrl.postValue(data.info.url.getUrl())
                                }
                            }
                        }
                        ui.hideDialog()
                    }

                    override fun onFail(e: Throwable?, errorMsg: String?) {
                        ui.hideDialog()
                    }
                })
        )

    }

    fun reportCourseTrain(type: String, trainTime: Int, calorie:String) {
        addSubscribe(HttpClient.instance.apiService
            .reportCourseTrain(type, 1, trainTime, calorie)
            .compose(RxUtils.compose())
            .subscribeWith(object : ApiResponse<Void>() {
                override fun success(data: Void?, msg: String?) {

                }

                override fun onFail(e: Throwable?, errorMsg: String?) {

                }
            })
        )
    }

    /**
     * 训练结束 轮播图
     */
    fun getSportEndVideo() {
        addSubscribe(
            HttpClient.instance.create(IApiService::class.java)
            .getSportEndVideo()
            .doOnSubscribe { ui.showDialog() }
            .subscribeWith(object : ApiResponse<AppBanner>() {
                override fun success(data: AppBanner?, msg: String?) {
                    imageUrl.postValue(data?.info?.url)
                }
                override fun onFail(e: Throwable?, errorMsg: String?) {

                }
            })
        )

    }


}